import axios from 'axios'
import config from "../../config";

const baseURL = process.env.NODE_ENV === 'development' ? config.baseURL.dev : config.baseURL.pro

class HttpRequest {
    constructor(baseURL) {
        this.baseURL = baseURL
    }
    //信息配置项 特别是请求的时用
    getInsideConfig() {
        const config =
        {
            baseURL: this.baseURL,
            header: {

            }
        }
        return config
    }
    intercerptors(instance) {
        instance.interceptors.request.use(config => {
            // Do something before request is sent
            return config;
        }, error => {
            // Do something with request error
            return Promise.reject(error);
        });
        instance.interceptors.response.use(response => {
            // Do something before response is sent
            // console.log(response)
            return response;
        }, error => {
            console.log(error)
            // Do something with response error
            return Promise.reject(error);
        });
    }
    request(options) {
        const instance = axios.create()
        options = { ...this.getInsideConfig(), ...options }
        this.intercerptors(instance)
        return instance(options)
    }
}
export default new HttpRequest(baseURL)